package tms.view;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import tms.controler.LoginController;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class LoginView extends JPanel {

    private static final long serialVersionUID = 1L;

    private JTextField txtEmail;
    private JLabel lblpassword;
    private JPasswordField txtPassword;
    private JButton btnOk;
    private JButton btnNewUser;

    public LoginView() {

        JLabel lblprofile = Tools.createLabel("Login to Travel System", Tools.TAHOMA_BOLD_16);
        JLabel lblemail = Tools.createLabel("Email");
        txtEmail = Tools.createTextField("");
        lblpassword = Tools.createLabel("Password");
        txtPassword = Tools.createPasswordField();

        btnOk = Tools.createButton("OK");
        btnOk.setDefaultCapable(true);
        btnOk.setPreferredSize(new Dimension(80, 20));

        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (checkLoginInfo()) {
                    boolean res = new LoginController().logIn(getEmail(), getPassword());
                    if (res) {
                        JOptionPane.showMessageDialog(null, "Log in success");
                        closeMe();
                    } else {
                        JOptionPane.showMessageDialog(null,
                                " Wrong email or password ! Please retype email and password");
                    }
                }
            }
        });
        txtEmail.addActionListener(btnOk.getActionListeners()[0]);
        txtPassword.addActionListener(btnOk.getActionListeners()[0]);

        btnNewUser = Tools.createButton("New User");
        btnNewUser.setPreferredSize(new Dimension(100, 20));
        setLayout(new FormLayout(new ColumnSpec[] {
                ColumnSpec.decode("default:grow"),
                ColumnSpec.decode("130px"),
                FormSpecs.LABEL_COMPONENT_GAP_COLSPEC,
                ColumnSpec.decode("130px"),
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("default:grow")},
            new RowSpec[] {
                RowSpec.decode("default:grow"),
                RowSpec.decode("28px"),
                FormSpecs.PARAGRAPH_GAP_ROWSPEC,
                RowSpec.decode("23px"),
                FormSpecs.RELATED_GAP_ROWSPEC,
                RowSpec.decode("23px"),
                FormSpecs.PARAGRAPH_GAP_ROWSPEC,
                RowSpec.decode("27px"),
                RowSpec.decode("default:grow")}));

        add(lblprofile, "2, 2, 3, 1, fill, fill");
        add(lblemail, "2, 4, left, center");
        add(txtEmail, "4, 4, fill, fill");
        add(lblpassword, "2, 6, left, center");
        add(txtPassword, "4, 6, fill, fill");
        add(btnOk, "2, 8, fill, fill");
        add(btnNewUser, "4, 8, fill, fill");

        btnNewUser.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                new RegisterView(null, false).setVisible(true);
                closeMe();
            }
        });

    }

    public void closeMe() {
        Container container = this.getTopLevelAncestor();
        while (!(container instanceof JDialog)) {
            container = this.getParent();
        }
        ((JDialog) container).dispose();
    }

    public boolean checkLoginInfo() {
        if (getEmail().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Please type your email");
            txtEmail.requestFocus();
            return false;
        }
        if (getPassword().trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "Please type your password");
            txtPassword.requestFocus();
            return false;
        }

        return true;
    }

    /**
     * Get the contents of the email field.
     *
     * @return The value of the email field
     */
    private String getEmail() {
        return txtEmail.getText();
    }

    /**
     * Get the password from the password field.
     *
     * @return The password as string
     */
    private String getPassword() {
        return String.copyValueOf(txtPassword.getPassword());
    }
}